/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kebut.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import kebut.file.FileProcessor;
import kebut.file.SoundPlayer;
import kebut.logic.stegano;
import kebut.logic.vinegere;
import kebut.ui.UI;

/**
 *
 * @author YLAW
 */
public class MainKebut {

    public static FileProcessor fileProcessor;
    public static SoundPlayer soundPlayer;
    public static stegano stegano;
    public static vinegere vinegere;
    public static UI ui;
    
    public static void init() {
        ui = new UI();
        FileProcessor.init();
    }
    
    public static void main(String[] args) {
        byte[] awal = new byte[1];
        
        try {
            //embed
            File fsm = new File("d://Beyonce - If I Were A Boy.wav");
            InputStream in = new FileInputStream(fsm);
            awal = new byte[(int)fsm.length()];
            in.read(awal,0,(int)fsm.length());
            
            File fs = new File("d://dpada.pdf");
            InputStream ins = new FileInputStream(fs);
            //out.print(b[0]);
            //System.out.println(fs.length());
            byte[] b4 = new byte[(int)fs.length()];
            ins.read(b4, 0, (int)fs.length());
            
            //perlu untuk menghitung payload untuk menentukan ukuran file yang boleh disisipkan
            //perhitungan payload ada di stegano totalSizePlainText(), tapi itu butuh berapa bit LSB untuk embed
            
            //untuk enkripsi
            // parameter
            // 1. plaintext (file yang mau dimasukkan) dalam byte[]
            // 2. true untuk enkripsi
            // 3. key
            vinegere = new vinegere(b4, true, "123000");
            vinegere.encrypt();
            byte[] b5 = new byte[(int)fs.length()];
            b5 = vinegere.getCipher();
            
            int i = fs.getName().length() - 1;
            while (fs.getName().charAt(i) != '.') {
                i--;
            }
            
            //ini untuk embed
            // parameter
            // 1. plaintext (file yang mau dimasukkan) dalam byte[]
            // 2. file sound dalam byte[]
            // 3. extension
            // 4. key
            // 5. status
            // kalau mau set data tinggal panggil variabel saja soalnya sudah diset public
            stegano = new stegano(b5, awal,fs.getName().substring(i+1, fs.getName().length()),"123000",3);
            stegano.encrypt();
            
            if (!stegano.isError) {
                FileOutputStream outs = new FileOutputStream("lagu.txt");
                outs.write(vinegere.getCipher());
                outs.close();

                FileOutputStream out = new FileOutputStream("lagu.wav");
                out.write(stegano.embedByte);
                out.close();
            } else {
                System.out.println("File Size not enough");
            }
            
            
            //extract
            File fss = new File("lagu.wav");
            InputStream inw = new FileInputStream(fss);
            awal = new byte[(int)fss.length()];
            inw.read(awal,0,(int)fss.length());
             //ini untuk extract
            // parameter
            // 1. plaintext (file yang mau dimasukkan) dalam byte[]
            // 2. key
            // 3. status
            stegano = new stegano(awal,"abcd",3);
            stegano.decrypt();
            inw.close();
            
            //untuk dekripsi
            // parameter
            // 1. ciphertext dalam byte[] yang berasal dari hasil ekstrak
            // 2. false untuk dekripsi
            // 3. key
            vinegere = new vinegere(stegano.extractByte, false, "abcd");
            vinegere.decrypt();
            
            FileOutputStream outsss = new FileOutputStream("hasil.txt");
            outsss.write(stegano.extractByte);
            outsss.close();
            
            if (stegano.extension != null) {
                FileOutputStream outss = new FileOutputStream("steganx." + stegano.extension);
                outss.write(vinegere.getPlain());
                outss.close();
            } else {
                FileOutputStream outss = new FileOutputStream("steganx");
                outss.write(vinegere.getPlain());
                outss.close();
            }

        } catch (IOException ex) {
            Logger.getLogger(MainKebut.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
}
